/****************************************************
 * 
 * home soft 版权所有.
 * 
 *****************************************************/

package com.homesoft.runtime.core;

import com.homesoft.runtime.log.ILog;
import com.homesoft.runtime.log.LogLevel;
import com.homesoft.runtime.log.LogManager;

/**
 * @author lihg
 * @data 2012-9-12
 */
public class RuntimeManager {
	private static ILog logger = LogManager.getLogger(RuntimeManager.class);;
	private static IRuntimeInstance _instance = null;
	private static IRuntimeInstanceFactory _factory = null;
	static {
		_factory = new DefaultRuntimeInstanceFactory();
	}

	public static IRuntimeInstance getInstace() {
		return _instance;
	};

	public static IRuntimeInstanceFactory getInstaceFactory() {
		return _factory;
	}

	public static boolean isStarted() {
		return _instance != null;
	}

	/**
	 * 启动运行时, 根目录为当前线程ClassLoader的根目录
	 */
	public static void start() {
		// start(GTPConfigurationManager.AppDirectory);
		start("");
	}

	public static void start(String rootPath) {
		if (isStarted()) {
			logger.writeTraceLog(LogLevel.Error, "runtime重复启动");
			// throw new
			// GTPRuntimeException(RuntimeErrorCode.REPEAT_START_RUNTIME);
		}
		// 设置Runtime启动路径下的资源文件路径
		// string runtimeResourcePath = Path.Combine(rootPath, "Biz");
		// ResourceBundleManager.SetRuntimeResourcePath(runtimeResourcePath);
		IRuntimeInstance inst = getInstaceFactory().Create();
		_instance = inst;
		inst.start();
	}

	// / <summary>
	// / 关闭运行时
	// / </summary>
	public static void stop() {
		if (isStarted()) {
			IRuntimeInstance inst = _instance;
			_instance = null;
			inst.dispose();
		}
	}

	public static void restart() {
		stop();
		start();
	}

	public static void restart(String rootPath) {
		stop();
		start(rootPath);
	}
}
